package com.sprouting.ops.controller;

import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.model.OpsDeployLink;
import com.sprouting.ops.model.OpsEnv;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.service.OpsDeployLinkService;
import com.sprouting.ops.service.OpsEnvService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 部署关联信息表;(OpsDeployLink)(OPS_DEPLOY_LINK) 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-07-30 22:34:19
 */
@Slf4j
@RestController
@RequestMapping("opsDeployLink")
public class OpsDeployLinkController {

    @Resource
    private OpsDeployLinkService opsDeployLinkService;
    @Resource
    private OpsEnvService opsEnvService;



    /**
     * 保存部署关联信息表
     * @param opsDeployId 部署id
     * @param envId 环境id
     * @return
     */
    @PostMapping("saveOpsDeployLink")
    @IdempotentSingle
    public ResultJson saveOpsDeployLink(String opsDeployId, String envId){
        if (CheckTool.checkNull(opsDeployId, envId)){
            return ResultJson.errorCheck();
        }
        OpsDeployLink opsDeployLink = opsDeployLinkService.getByOpsDeployIdAndEnvId(opsDeployId, envId);
        if (opsDeployLink != null){
            return ResultJson.error("已经存在相同的绑定关系，无需重复绑定");
        }

        OpsDeployLink deployLink = new OpsDeployLink();
        deployLink.setId(IdTool.getId());
        deployLink.setOpsDeployId(opsDeployId);
        deployLink.setEnvId(envId);

        OpsEnv env = opsEnvService.getById(envId);
        deployLink.setProductId(env.getProductId());

        opsDeployLinkService.save(deployLink);
        return ResultJson.success(deployLink);
    }




}
